package common.util;

import common.util.matcher.AntPathRequestMatcher;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 需要用户登录（即需要token），但所有角色都可以访问的url
 * 不会绕开spring security验证，相当于进入filter后再放行
 *
 * @author 米泽鹏
 * @since 2022-3-23 下午 10:58
 */
public class CommonUrlAntPatternUtil {

	private static final List<String> ANT_PATTERNS = new ArrayList<>();
	private static final List<AntPathRequestMatcher> ANT_PATH_REQUEST_MATCHERS = new ArrayList<>();

	static {
		ANT_PATTERNS.add(SecurityConstants.LOGOUT_PROCESSING_URL);
		ANT_PATTERNS.add("/system/sys-myself/**");
		ANT_PATTERNS.add("/system/common/**");
		ANT_PATTERNS.add("/system/sys-business-code/readOnly/getAllBusinessCodeFromCache");
		ANT_PATTERNS.add("/system/sys-web-config/saveUserConfig");
		for (String antPattern : ANT_PATTERNS) {
			ANT_PATH_REQUEST_MATCHERS.add(new AntPathRequestMatcher(antPattern));
		}
	}

	private CommonUrlAntPatternUtil() {
		throw new AssertionError("不可实例化");
	}

	public static List<String> getAllPatterns() {
		return ANT_PATTERNS;
	}

	public static List<AntPathRequestMatcher> getAllMatchers() {
		return ANT_PATH_REQUEST_MATCHERS;
	}

	public static boolean isCommonUrl(HttpServletRequest request) {
		for (AntPathRequestMatcher matcher : ANT_PATH_REQUEST_MATCHERS) {
			if (matcher.matches(request)) {
				return true;
			}
		}
		return false;
	}

}
